#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

struct student_info{
    char name[101];
    int score;
};

bool cmp_in(student_info a, student_info b);
bool cmp_de(student_info a, student_info b);

int main(int argc, char const *argv[]){
    int n;
    while(cin >> n){
    	student_info data[n];
        int control;
        cin >> control;
        for (int i = 0; i < n; i++)
            cin >> data[i].name >> data[i].score;
        if(control == 0)
            sort(data, data+n, cmp_de);
        else
            sort(data, data+n, cmp_in);
        for (int i = 0; i < n; i++)
            cout << data[i].name << ' '  << data[i].score << endl;
    }
    return 0;
}

bool cmp_de(student_info a, student_info b){
    return a.score > b.score;
}

bool cmp_in(student_info a, student_info b){
    return a.score < b.score;
}

